package cn.wenlihelp.controller.power;

import cn.wenlihelp.annotations.Authorize;
import cn.wenlihelp.bean.Account;
import cn.wenlihelp.bean.Role;
import cn.wenlihelp.model.Result;
import cn.wenlihelp.service.AccountService;
import cn.wenlihelp.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/account")
public class AccountController {

    private int errorCodeRoot = 10000;

    private String model = "account";
    private String modelPath = "power/" + model;


    @Resource
    private AccountService accountService;
    @Resource
    private RoleService roleService;

    @Authorize(authorization = "ACCOUNT_MANAGER",role = {"ADMIN","ACCOUNT_MANAGER"})
    @RequestMapping
    public ModelAndView home() {
        return new ModelAndView(modelPath + "/index");
    }

    @Authorize(authorization = "ACCOUNT_ADD",role = {"ADMIN","ACCOUNT_MANAGER"})
    @GetMapping("/add")
    public ModelAndView addPage() {
        return new ModelAndView(modelPath + "/add");
    }


    @Authorize(authorization = "ACCOUNT_UPDATE",role = {"ADMIN","ACCOUNT_MANAGER"})
    @GetMapping("/{id}")
    public ModelAndView updatePage(@PathVariable("id") Long id, ModelMap modelMap) {
        Account account = accountService.findById(id);
        modelMap.addAttribute(model, account);
        return new ModelAndView(modelPath + "/update");
    }

    @Authorize(authorization = "ACCOUNT_ROLE_MAPPING",role = {"ADMIN","ACCOUNT_MANAGER"})
    @GetMapping("/role/{id}")
    public ModelAndView roleMapping(@PathVariable("id") Integer id, ModelMap modelMap) {
        modelMap.addAttribute("account",this.accountService.findById(id.longValue()));
        modelMap.addAttribute("roles",this.roleService.findAll(null));
        List<Role> roleList = this.roleService.findByAccountId(id);
        Map<Integer,Role> roleMap =  roleList.stream().collect(Collectors.toMap(Role::getId,r -> r));
        modelMap.addAttribute("roleMappings",roleMap);
        return new ModelAndView(modelPath + "/roleMapping");
    }

    @Authorize(authorization = "ACCOUNT_ROLE_MAPPING",role = {"ADMIN","ACCOUNT_MANAGER"})
    @PostMapping("/role/{id}")
    public Result roleMapping(@PathVariable("id") Integer accountId,@RequestParam(name = "roleIds[]",required = false)Integer[] roleIds) {
        try {
            this.accountService.roleMapping(accountId,roleIds);
            return Result.success("关联成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result().setCode(this.errorCodeRoot+4).setMsg("关联失败");
    }

    //region 查询账号
    @GetMapping(produces = "application/json")
    @Authorize(authorization = "ACCOUNT_MANAGER",role = {"ADMIN","ACCOUNT_MANAGER"})
    public Object findAll(Account account,
                          @RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10") int limit) {
        PageInfo<Account> pageInfo = accountService.findAll(page, limit, account);
        return Result.success(pageInfo.getTotal(), pageInfo.getList());
    }
    //endregion

    //region 批量删除账号
    @DeleteMapping
    @Authorize(authorization = "ACCOUNT_DEL",role = {"ADMIN","ACCOUNT_MANAGER"})
    public Result deleteByIds(@RequestParam("ids[]") Integer[] ids) {

        if (accountService.deleteByIds(ids)) {
            return Result.success("删除成功");
        } else {
            return new Result().setCode(errorCodeRoot + 1).setMsg("删除失败");
        }
    }
    //endregion

    //region 新增账号
    @PutMapping
    @Authorize(authorization = "ACCOUNT_ADD",role = {"ADMIN","ACCOUNT_MANAGER"})
    public Result add(@RequestBody Account account) {
        if (accountService.insert(account)) {
            return Result.success("新增成功");
        } else {
            return new Result().setCode(errorCodeRoot + 2).setMsg("新增失败");
        }
    }
    //endregion


    //region 修改账号
    @PostMapping
    @Authorize(authorization = "ACCOUNT_UPDATE",role = {"ADMIN","ACCOUNT_MANAGER"})
    public Result updataAccount(@RequestBody Account account) {
        if (accountService.updateByPrimaryKey(account)) {
            return Result.success("修改成功");
        } else {
            return new Result().setCode(errorCodeRoot + 3).setMsg("新增失败");
        }
    }
    //endregion


}
